package com.dsg.token;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * ID生成器
 */
public class IDUtil {

    public static final String ID_PREFIX = "BLUE";
    private static final String DEFAULT_FORMAT_STRING = "yyyyMMddHHmmssSSS";
    private static final DateTimeFormatter format = DateTimeFormatter.ofPattern(DEFAULT_FORMAT_STRING);

    /**
     * 生成ID
     *
     * @return
     */
    public static String generateID() {
        LocalDateTime localDateTime = LocalDateTime.now();
        String time = localDateTime.format(format);
        return ID_PREFIX + time + getRandomString(7);
    }

    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < length; ++i) {
            int number = random.nextInt(3);
            long result = 0L;
            switch (number) {
                case 0:
                    result = Math.round(Math.random() * 25.0D + 65.0D);
                    sb.append(String.valueOf((char) ((int) result)));
                    break;
                case 1:
                    result = Math.round(Math.random() * 25.0D + 97.0D);
                    sb.append(String.valueOf((char) ((int) result)));
                    break;
                case 2:
                    sb.append(String.valueOf((new Random()).nextInt(10)));
            }
        }

        return sb.toString();
    }
}
